package com.xm.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

@Slf4j
public class ClientListener implements Runnable {
    private final Socket socket;
    private final int i;
    public ClientListener(Socket socket, int i) {
        this.socket = socket;
        this.i = i;
    }

    @Override
    public void run() {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String request = in.readLine();
            log.info("Received from client[{}]：{}" , i, request);

            out.println("Hello from server！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
